package com.acce.gxsc.page.supervisionReport.supervisionReportPresenter;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.App;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetSupervisionListBean;
import com.acce.gxsc.page.supervisionReport.allDetail.SupervisionReportAllDetailActivity;
import com.acce.sdk.User;
import com.acce.sdk.base.mvp.BaseMvpActivity;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */
public class SupervisionReportActivity extends BaseMvpActivity<SupervisionReportView, SupervisionReportPresenter> implements SupervisionReportView {

    View rootView;
    LayoutInflater mInflater;
    String dutyId;

    @Bind(R.id.tv_total_a)
    TextView mTvTotalA;
    @Bind(R.id.tv_total_b)
    TextView mTvTotalB;
    @Bind(R.id.tv_total_c)
    TextView mTvTotalC;
    @Bind(R.id.tv_total_d)
    TextView mTvTotalD;
    @Bind(R.id.tv_total_e)
    TextView mTvTotalE;
    @Bind(R.id.tv_total_f)
    TextView mTvTotalF;
    @Bind(R.id.tv_total_g)
    TextView mTvTotalG;
    @Bind(R.id.tv_total_h)
    TextView mTvTotalH;
    @Bind(R.id.tv_duty_id)
    TextView mTvDutyId;
    @Bind(R.id.tv_gate_name)
    TextView mTvGateName;
    @Bind(R.id.tv_real_name)
    TextView mTvRealName;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_supervision_report);
        ButterKnife.bind(this);
        init();
        mPresenter.loadSupervisionReport(dutyId);
    }

    @Override
    protected SupervisionReportPresenter createPresenter() {
        return new SupervisionReportPresenter();
    }

    private void init() {
        dutyId = getIntent().getStringExtra("dutyId");
        mInflater = LayoutInflater.from(this);
        mTvDutyId.setText(dutyId);
        mTvGateName.setText(Tools.getString(App.gateName));
        mTvRealName.setText(Tools.getString(User.getInstance().getRealName()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    NetSupervisionListBean.StatInfo si;

    @Override
    public void loadSupervisionReportSuccess(ArrayList<NetSupervisionListBean.StatInfo> statInfo) {
        if (statInfo == null || statInfo.isEmpty()) {
            si = new NetSupervisionListBean.StatInfo("0", "0", "0", "0", "0", "0", "0", "0");
        } else {
            si = statInfo.get(0);
        }
        mTvTotalA.setText(Tools.getString(si.totalA));
        mTvTotalB.setText(Tools.getString(si.totalB));
        mTvTotalC.setText(Tools.getString(si.totalC));
        mTvTotalD.setText(Tools.getString(si.totalD));
        mTvTotalE.setText(Tools.getString(si.totalE));
        mTvTotalF.setText(Tools.getString(si.totalF));
        mTvTotalG.setText(Tools.getString(si.totalG));
        mTvTotalH.setText(Tools.getString(si.totalH));
    }

    @Override
    public void printSuccess() {

    }

    @OnClick({R.id.btn_detail, R.id.btn_print})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_detail:
                Intent intent = new Intent(this, SupervisionReportAllDetailActivity.class);
                intent.putExtra("dutyId", dutyId);
                startActivity(intent);
                break;
            case R.id.btn_print:
                printOrder();
                break;
        }
    }

    private void printOrder() {
        ScrollView rootView = (ScrollView) mInflater.inflate(R.layout.supervision_report_dialog_supervision, (ViewGroup) this.findViewById(R.id.dialog));
        final LinearLayout llContent = ButterKnife.findById(rootView, R.id.ll_content);
        final TextView tvClasses = ButterKnife.findById(rootView, R.id.tv_classes);
        final TextView tvTollCollector = ButterKnife.findById(rootView, R.id.tv_toll_collector);
        final TextView tvDate = ButterKnife.findById(rootView, R.id.tv_date);
        final TextView tvTotalA = ButterKnife.findById(rootView, R.id.tv_total_a);
        final TextView tvTotalB = ButterKnife.findById(rootView, R.id.tv_total_b);
        final TextView tvTotalC = ButterKnife.findById(rootView, R.id.tv_total_c);
        final TextView tvTotalD = ButterKnife.findById(rootView, R.id.tv_total_d);
        final TextView tvTotalE = ButterKnife.findById(rootView, R.id.tv_total_e);
        final TextView tvTotalF = ButterKnife.findById(rootView, R.id.tv_total_f);
        final TextView tvTotalG = ButterKnife.findById(rootView, R.id.tv_total_g);
        final TextView tvTotalH = ButterKnife.findById(rootView, R.id.tv_total_h);
        tvClasses.setText(MessageFormat.format("班次：{0}", App.onTime));
        tvTollCollector.setText(MessageFormat.format("操作员：{0}", User.getInstance().getRealName()));
        tvDate.setText(DateUtils.getTime1());

        if (si == null) {
            si = new NetSupervisionListBean.StatInfo("0", "0", "0", "0", "0", "0", "0", "0");
        }
        tvTotalA.setText(Tools.getString(si.totalA));
        tvTotalB.setText(Tools.getString(si.totalB));
        tvTotalC.setText(Tools.getString(si.totalC));
        tvTotalD.setText(Tools.getString(si.totalD));
        tvTotalE.setText(Tools.getString(si.totalE));
        tvTotalF.setText(Tools.getString(si.totalF));
        tvTotalG.setText(Tools.getString(si.totalG));
        tvTotalH.setText(Tools.getString(si.totalH));
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder
                .setView(rootView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.printOrder(llContent);
                    }
                }).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }
}
